#include "Texture.h"


Texture::Texture(void) 
{
	m_pos = 0;
	m_rot = 0;
	m_scale = 0;
	m_bClickable = false;
	m_bCentered = true;
	pEngine = DX9Engine::getInstance();
	pInput = Input::getInstance();

}

Texture::~Texture(void) {}

/** create a simple texture to render */
void Texture::initialize(LPCWSTR fileName)
{
	int i=0;
	while(fileName[i] != 0)
	{
		i++;
	}

	m_id = pEngine->createSprite(fileName, i);
	// set texture rect
	m_bounds = pEngine->getBounds(m_id);
}

/** initialize texture with data */
void Texture::initialize(LPCWSTR fileName, V2DF pos, float rot, float scale, bool centered)
{
	int i=0;
	while(fileName[i] != 0)
	{
		i++;
	}

	m_id = pEngine->createSprite(fileName, i);

	m_pos = pos;
	m_rot = rot;
	m_scale = scale;
	m_bCentered = centered;
	// set texture rect
	m_bounds = pEngine->getBounds(m_id);
}

/** draw from top left */
void Texture::draw()
{
	pEngine->renderTexture(m_id, m_pos, m_scale, m_rot, 0, m_bCentered, 255, 255, 255);
}

/** draw texture at position */
void Texture::draw(V2DF pos, float rot, float scale, bool centered)
{
	pEngine->renderTexture(m_id, pos, scale, rot, 0, centered, 255, 255, 255);
}

/** draw from sprite sheet rect */
void Texture::draw(RECT sheet, V2DF pos, float rot, float scale, bool centered)
{
	pEngine->renderTexture(m_id, pos, scale, rot, &sheet, centered, 255, 255, 255);
}

/** set texture position */
void Texture::updatePosition(V2DF a_pos)
{
	m_pos = a_pos;
}

/** set texture rotation */
void Texture::updateRotation(float a_rot)
{
	m_rot = a_rot;
}

/** set texture scale */
void Texture::updateScale(float a_scale)
{
	m_scale = a_scale;
}

/** update bounds with textures position */
void Texture::updateRect()
{
	m_bounds = pEngine->getBounds(m_id);
}

/** check if texture has been clicked */
bool Texture::hovered()
{
	int h = m_bounds.bottom*0.5;
	int w = m_bounds.right*0.5;
	if(	pInput->getMousePos().y < (m_pos.y+h)
	&&	pInput->getMousePos().y > (m_pos.y-h)
	&&	pInput->getMousePos().x < (m_pos.x+w)
	&&	pInput->getMousePos().x > (m_pos.x-w) )
	{
		return true;
	}
	return false;
}